/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package semanticsearch.engine;

import java.util.ArrayList;
import java.util.Arrays;
import semanticsearch.operation.SearchOperation;
import semanticsearch.util.sql.SQLOperation;

/**
 *
 * @author Pavithre
 */
public class Engine {
    
    private SQLOperation SQLOperation;
    private SearchOperation searchOperation;
    private ArrayList<String> excludeList;
    
    public Engine(){
        
        SQLOperation = new SQLOperation();
        searchOperation = new SearchOperation();
        
        excludeList = new ArrayList<String>();
        
        excludeList.add("What"); excludeList.add("what"); excludeList.add("WHAT");
        excludeList.add("is"); excludeList.add("Is"); excludeList.add("IS");
        excludeList.add("am"); excludeList.add("AM"); excludeList.add("Am");
        excludeList.add("are"); excludeList.add("Are"); excludeList.add("ARE");
        excludeList.add("of"); excludeList.add("OF"); excludeList.add("Of");
        excludeList.add("the"); excludeList.add("THE"); excludeList.add("The");
        excludeList.add("Who"); excludeList.add("who"); excludeList.add("WHO");
        excludeList.add("WHY"); excludeList.add("why"); excludeList.add("Why");
        excludeList.add("When"); excludeList.add("when"); excludeList.add("WHEN");
        excludeList.add("he"); excludeList.add("He"); excludeList.add("HE");
        excludeList.add("SHE"); excludeList.add("she"); excludeList.add("She");
        excludeList.add("They"); excludeList.add("they"); excludeList.add("THEY");
        excludeList.add("That"); excludeList.add("that"); excludeList.add("THAT");
        excludeList.add("WHERE"); excludeList.add("where"); excludeList.add("where");
        excludeList.add("were"); excludeList.add("WERE"); excludeList.add("Were");
        excludeList.add("their"); excludeList.add("Their"); excludeList.add("THEIR");
        excludeList.add("in"); excludeList.add("In"); excludeList.add("IN");
        excludeList.add("on"); excludeList.add("On"); excludeList.add("ON");
        excludeList.add("as"); excludeList.add("As"); excludeList.add("AS");
        excludeList.add("be"); excludeList.add("be"); excludeList.add("be");
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        ArrayList<String> paths = new ArrayList<String>();
        paths.add("E:\\Search_Material\\text1.txt");
        paths.add("E:\\Search_Material\\text2.txt");
        paths.add("E:\\Search_Material\\text3.txt");
        paths.add("E:\\Search_Material\\text4.txt");
        paths.add("E:\\Search_Material\\text5.txt");
        paths.add("E:\\Search_Material\\text6.txt");
        
        String searchString = "Indian elephants";
        
        Engine engine = new Engine();
        ArrayList<String>  list = engine.doSearch(searchString, paths);
        
        for (int i = 0; i < list.size(); i++) {
            
            System.out.println(list.get(i));
        }
        
    }
    
    /**
     * Do search on given <code>filePaths</code> related to <code>keyWord</code>
     * @param keyWord
     * @param filePaths
     * @return 
     */
    public ArrayList<String> doSearch(String keyWord, ArrayList<String> filePaths){
        
        // Prepare keyWord
        ArrayList<String> searchQuary = prepareSearchQuary(keyWord);
        
        // Get list synonyms
        ArrayList<String> searchResult = null;
        ArrayList<String> classList = null;
        classList = SQLOperation.getClasses(searchQuary);
        
        // Get search results
        searchResult = searchOperation.getSearchResultsFromClasses(classList, filePaths);
        
        return searchResult;
    }
    
    /**
     * Stop search.
     */
    public void stopSearch(){
        searchOperation.stopSearch();
    }
    
    /**
     * Prepare search query form key words.
     * @param keyWord
     * @return 
     */
    private ArrayList<String> prepareSearchQuary(String keyWord){
        
        boolean excludeItem;
        
        ArrayList<String> searchQuary = new ArrayList<String>();
        String[] keyWordChunk = keyWord.split(" ");
        
        for (int i = 0; i < keyWordChunk.length; i++) {
            excludeItem = false;
            for (int j = 0; j < excludeList.size(); j++) {
                
                if (excludeList.get(j).equals(keyWordChunk[i])) {
                    excludeItem = true;
                }
            }
            
            if (!excludeItem) {
                searchQuary.add(keyWordChunk[i]);
            }
        }
        
        if (searchQuary.size() <= 1) {
            searchQuary = new ArrayList<String>(Arrays.asList(keyWordChunk));
        }
        
        return searchQuary;
    }
    
    /**
     * Check search is running.
     * @return 
     */
    public boolean isSearching(){
        
        return false;
    }
}
